package cn.topcodes.tcsf.wechat.web.controller;

import cn.topcode.unicorn.utils.I18nUtil;
import cn.topcodes.tcsf.admin.commons.api.controller.admin.AdminController;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.commons.domain.Page;
import cn.topcodes.tcsf.wechat.domain.entity.WechatMp;
import cn.topcodes.tcsf.wechat.domain.entity.WechatUser;
import cn.topcodes.tcsf.wechat.domain.vo.WechatUserVo;
import cn.topcodes.tcsf.wechat.domain.form.WechatUserForm;
import cn.topcodes.tcsf.wechat.service.WechatMpService;
import cn.topcodes.tcsf.wechat.service.WechatUserService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 微信用户管理控制器
 * @author unicorn
 * @date 2017-08-15 17:19:03
 */
@Controller
@RequestMapping("/admin/wechat/user")
public class WechatUserController extends AdminController<WechatUser,WechatUserVo,WechatUserForm> {

	@Autowired
	private WechatMpService wechatMpService;

	@Autowired
	private WechatUserService wechatUserService;

	@Override
	protected String getPageFolder() {
		return "wechat/admin/wechat-user";
	}

	@Override
	public String index(HttpServletRequest request, Model model) {
		List<WechatMp> wechatMps = wechatMpService.findAll();
		model.addAttribute("wechatMps", wechatMps);
		return super.index(request, model);
	}

	@ResponseBody
	@RequestMapping(value = "sync-from-mp", method = RequestMethod.POST)
	public ApiResponseEntity syncFormMp(@RequestParam String mpId) {
		this.wechatUserService.syncAllUserFromMp(mpId);
		return ApiResponseEntity.buildOK();
	}

	@ResponseBody
	@RequestMapping(value = "page-list-ex", method = RequestMethod.GET)
	public ApiResponseEntity<Page<WechatUserVo>> pageListEx(
			@RequestParam(required = true) Long wechatMpId,
			@RequestParam(required = false) String nickname,
			@RequestParam(required = false) String openId,
			HttpServletRequest request,
			@ApiParam("页号")
			@RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNumber,
			@ApiParam("每页条数")
			@RequestParam(value = "size",required = false,defaultValue = "10") Integer pageSize
	) {
		Page<WechatUserVo> pageT = this.wechatUserService.findPage(wechatMpId, nickname, openId, pageNumber.intValue(), pageSize.intValue());
		I18nUtil.processAnnotation(pageT.getRows(), request);
		return ApiResponseEntity.buildOK(pageT);
	}
}
